﻿using System;
using System.Xml;
using Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace UIComponentSprite.Building
{
    public class BuildingBaseInfoSprite : UserControlledSprite
    {
        public Texture2D ForceColorTexture2D { get; set; }
        public Rectangle DrowForceColorPosition = new Rectangle(149, 89, 18, 18);
        public Texture2D ControllingImage { get; set; }
        /// <summary>
        /// 是否玩家控制的城池，以便显示不同的窗口信息
        /// </summary>
        private bool Controlling { get; set; }
        public Rectangle TempRectangle = new Rectangle();//临时使用，避免方法体中局部声明导致大量创建对象，影响性能
        public short PreBuildingId;//上一个建筑城池Id
        public ImageTextSprite ForceText { get; set; }//势力名
        public Color FoceColor = Color.White;//势力颜色
        public ImageTextSprite NameText { get; set; }//城池名称
        public XnaTextSprite AgricultureText;//农业
        public XnaTextSprite PopulationText;//人口
        public XnaTextSprite TechnologyText;//技术
        public XnaTextSprite CommerceText;//商业
        public XnaTextSprite EnduranceText;//城池耐久
        public XnaTextSprite FoodText;//粮草
        public XnaTextSprite FundText;//资金
       
        /// <summary>
        /// 当前要显示的城池
        /// </summary>
        public Domain.Building Building { get; set; }
        public Point DisplayOffset = new Point();
        public int Left;
      
        public int Top;
       

        public BuildingBaseInfoSprite()
        {
        }
        public override void Draw(GameTime gameTime)
        {
            if (!Showing)
                return;
           // SetTopLeftPoint(Mouse.GetState());
            
            SpriteBatch.Draw(this.Controlling ? this.ControllingImage : this.Image, DrawPosition, null, Color.White);
            SpriteBatch.Draw(this.ForceColorTexture2D, DrowForceColorPosition, null, this.FoceColor);
            this.NameText.Draw(gameTime);
            this.ForceText.Draw(gameTime);
            this.PopulationText.DrawString();
            this.AgricultureText.DrawString();
            this.CommerceText.DrawString();
            this.TechnologyText.DrawString();
            this.EnduranceText.DrawString();
            this.FoodText.DrawString();
            this.FundText.DrawString();
            
        }
        public override void Update(GameTime gameTime)
        {
            if (!Showing)
                return;
            this.Controlling = true; //building.Force.Controlling;
            SetTopLeftPoint(MouseState);
            TempRectangle.X = this.Left - DrawPosition.Width;
            TempRectangle.Y = this.Top - DrawPosition.Height;
            TempRectangle.Width = DrawPosition.Width;
            TempRectangle.Height = DrawPosition.Height;
            GameScreen.AdjustRectangleInViewport(ref TempRectangle);
            this.DisplayOffset.X = TempRectangle.X;
            this.DisplayOffset.Y = TempRectangle.Y;
            DrawPosition.X = this.DisplayOffset.X;
            DrawPosition.Y = this.DisplayOffset.Y;
            DrawPosition.Width = this.Controlling ? 388 : 194;
            DrawPosition.Height = 273;
            DrowForceColorPosition.X += this.DisplayOffset.X;
            DrowForceColorPosition.Y += this.DisplayOffset.Y;
           
            LoadPic();
            InitTextSprite(this.Building);
            this.ForceText.DrawPosition.X = 65 + this.DisplayOffset.X;
            this.ForceText.DrawPosition.Y = 87 + this.DisplayOffset.Y;
            this.NameText.DrawPosition.X = 65 + this.DisplayOffset.X;
            this.NameText.DrawPosition.Y = 38 + this.DisplayOffset.Y;
            this.PopulationText.Position.X = 63 + this.DisplayOffset.X;
            this.PopulationText.Position.Y = 136 + this.DisplayOffset.Y;
            this.AgricultureText.Position.X = 255 + this.DisplayOffset.X;
            this.AgricultureText.Position.Y = 169 + this.DisplayOffset.Y;
            this.CommerceText.Position.X = 255 + this.DisplayOffset.X;
            this.CommerceText.Position.Y = 147 + this.DisplayOffset.Y;
            this.TechnologyText.Position.X = 255 + this.DisplayOffset.X;
            this.TechnologyText.Position.Y = 125 + this.DisplayOffset.Y;
            this.EnduranceText.Position.X = 63 + this.DisplayOffset.X;
            this.EnduranceText.Position.Y = 210 + this.DisplayOffset.Y;
            this.FoodText.Position.X = 255 + this.DisplayOffset.X;
            this.FoodText.Position.Y = 37 + this.DisplayOffset.Y;
            this.FundText.Position.X = 255 + this.DisplayOffset.X;
            this.FundText.Position.Y = 59 + this.DisplayOffset.Y;

        }
        /// <summary>
        /// 鼠标移入事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void MouseMoveOnEventHandler(object args)
        {
            string[] arr = args.ToString().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            SetShowing(arr[0] == "1");
            this.Building = Domain.Building.Buildings[Convert.ToInt16(arr[1])];
            string[] point = arr[2].Split("|".ToCharArray());
            DisplayOffset.X = Convert.ToInt32(point[0]);
            DisplayOffset.Y = Convert.ToInt32(point[1]);

        }
        /// <summary>
        /// 鼠标移出事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void MouseMoveOutEventHandler(object args)
        {
            SetShowing(false);
        }
        protected override void LoadPic()
        {
            if (this.Image == null)
                this.Image = Game.Content.Load<Texture2D>(@"Images\views\building\baseInfo\Background");
            if (this.ControllingImage == null)
                this.ControllingImage = Game.Content.Load<Texture2D>(@"Images\views\building\baseInfo\BackgroundForControlling");
            if (this.ForceColorTexture2D == null)
                this.ForceColorTexture2D = Game.Content.Load<Texture2D>(@"Images\views\building\baseInfo\ForceColor");
        }
        /// <summary>
        /// 设置弹窗位置,根据窗口大小，防止显示到了窗口外面
        /// </summary>
        /// <param name="mouseState"></param>
        private void SetTopLeftPoint(MouseState mouseState)
        {
            if (mouseState.X < 670 && mouseState.Y < 300)
            {
                Left = GameScreen.ViewportSize.X - 100;
            }
            else
            {
                Left = 280;
            }
            Top = 20;
        }
        /// <summary>
        /// 初始化弹窗内容中的文字精灵
        /// </summary>
        /// <param name="building"></param>
        public void InitTextSprite(Domain.Building building)
        {
            if(building==null||this.PreBuildingId==building.Id)//不同建筑城池时，才需要重新初始化内容
                return;
            if (this.Building.Force != null)
            {
                this.FoceColor = this.Building.Force.Color;
                this.ForceText=new ImageTextSprite(Building.Force.Name,new Rectangle(65,87,60,22));
            }
            this.NameText = new ImageTextSprite(Building.Name,new Rectangle(65,38,70,22));
            this.PopulationText=new XnaTextSprite(building.Population.ToString(),new Vector2(63f,136f));
            this.AgricultureText = new XnaTextSprite(building.Agriculture.ToString(), new Vector2(255f, 169f));
            this.CommerceText = new XnaTextSprite(building.Commerce.ToString(), new Vector2(255f, 147f));
            this.TechnologyText = new XnaTextSprite(building.Technology.ToString(), new Vector2(255f, 125f));
            this.EnduranceText = new XnaTextSprite(building.Endurance.ToString(), new Vector2(63f, 210f));
            this.FoodText = new XnaTextSprite(building.Food.ToString(), new Vector2(255f, 37f));
            this.FundText = new XnaTextSprite(building.Fund.ToString(), new Vector2(255f, 59f));
            this.PreBuildingId = building.Id;
        }

    }
}
